<template>
  <div>
    <hr />
    <el-row :gutter="20">
      <el-col :span="10">
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item label="学生姓名">
            <el-input v-model="form.name"></el-input>
          </el-form-item>
          <el-form-item label="学生年龄">
            <el-input v-model="form.age"></el-input>
          </el-form-item>
          <el-form-item label="学生性别">
            <el-radio-group v-model="form.gender">
              <el-radio label="男"></el-radio>
              <el-radio label="女"></el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="学生专业">
            <el-select v-model="form.subjectsId" placeholder="请选择">
              <el-option
                v-for="item in subject"
                :key="item._id"
                :label="item.name"
                :value="item._id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="学生班级">
            <el-select v-model="form.classesId" placeholder="请选择">
              <el-option
                v-for="item in classes"
                :key="item._id"
                :label="item.name"
                :value="item._id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="addStudents">立即创建</el-button>
            <el-button>取消</el-button>
          </el-form-item>
        </el-form>
      </el-col>
      <el-col :span="6">
        <el-upload
          class="avatar-uploader"
          action="http://47.98.128.191:3000/images/uploadImages"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="form.imagePath" :src="form.imagePath" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-col>
    </el-row>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        name: "",
        age: "",
        gender: "",
        // subjectsId: "",
        // classesId: "",
        imagePath: "",
      },

      subjects: [],
      classes: [],
    };
  },
  created() {
    this.getSubjects();
  },
  watch: {
    "form.subjectsId"() {
      this.getClassesBySid();
    },
  },
  computed: {
    subject() {
      return this.$store.state.subjectsModule.subjects;
    },
  },
  methods: {
    async addStudents() {
      const res = await this.$api.students.add(this.form);
      if (res.code) {
        this.$message({
          message: "新增学生成功",
          type: "success",
        });
        this.$router.push("/home/studentList");
      } else {
        this.$message.error("新增学生失败");
      }
    },
    async getSubjects() {
     this.$store.dispatch("subjectsModule/getSubjectsAsync");
      // const res = await this.$api.subjects.get();
      // console.log(res);

      // if (res.code) {
      //   this.subjects = res.data.rows;
      // }
    },
    async getClassesBySid() {
      const res = await this.$api.classes.getBySid({
        subjectsId: this.form.subjectsId,
      });
      console.log(res);
      if (res.code) {
        this.classes = res.data.rows;
      }
    },

    handleAvatarSuccess(res, file) {
      this.form.imagePath = res.data[0];
      // this.imageUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
};
</script>

<style scoped>
.el-input {
  width: 280px;
}
.el-select {
  width: 280px;
}
hr {
  margin-bottom: 30px;
}
.avatar-uploader >>> .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader >>> .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
